package com.example.casper.Experiment2024.data;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.casper.Experiment2024.R;

public class BookDetailsActivity extends AppCompatActivity {
    @SuppressLint("CutPasteId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_book);

        Button saveButton = findViewById(R.id.saveButton);

        Intent intent = getIntent();
        if (intent != null) {
            // 从Intent中获取传递的数据
            String title = intent.getStringExtra("title");
            if (null != title) {
                int coverResourceId = intent.getIntExtra("coverResourceId",R.drawable.book_no_name);
                EditText editTextItemtitle= findViewById(R.id.titleText);
                editTextItemtitle.setText(title);
                EditText editTextItemcoverResourceId= findViewById(R.id.coverResourceIdText);
                editTextItemcoverResourceId.setText(coverResourceId);

            }
        }


        saveButton.setOnClickListener(v -> {
            Intent returnintent = new Intent();
            assert intent != null;
            returnintent.putExtra("position",intent.getIntExtra("position",0));
            EditText editTextItemtitle= findViewById(R.id.titleText);
            EditText editTextItemcoverResourceId= findViewById(R.id.coverResourceIdText);
            returnintent.putExtra("title", editTextItemtitle.getText().toString());
            returnintent.putExtra("coverResourceId", editTextItemcoverResourceId.getText().toString());
            setResult(Activity.RESULT_OK, returnintent);
            BookDetailsActivity.this.finish();
        });


        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }
}